<?php

namespace app\models;

use Yii;
use yii\db\Expression;
/**
 * This is the model class for table "notice".
 *
 * @property integer $id
 * @property string $title
 * @property integer $author
 * @property string $content
 * @property string $dt
 *
 * @property Member $author0
 */
class Notice extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'notice';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title', 'author', 'content'], 'required'],
            [['author'], 'integer'],
            [['content'], 'string'],
            [['dt'], 'safe'],
            [['title'], 'string', 'max' => 256],
            [['author'], 'exist', 'skipOnError' => true, 'targetClass' => Member::className(), 'targetAttribute' => ['author' => 'id']],
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getAuthor0()
    {
        return $this->hasOne(Member::className(), ['id' => 'author']);
    }
    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => '公告标题',
            'author' => '发布者',
            'content' => '公告内容',
            'dt' => '发布日期',
        ];
    }
    /**
     * @return \yii\db\ActiveQuery
     */
    public function addNotice($model)
    {
      $notice = new Notice;
      $notice->title=$model->title;
      $notice->content=$model->content;
      $notice->dt=new Expression('NOW()');
      $notice->author=1;
      if(!$notice->save()){
        return false;
      }
      return true;
    }
    public static function delNotice($keys){
      return Notice::deleteAll(['id'=>$keys]);
    }
}
